package controllers;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import models.User;

//import com.sun.java.util.jar.pack.Package.File;
import com.sun.xml.internal.txw2.Document;

import play.libs.Codec;
import play.mvc.Controller;

public class Photo extends Controller {
	private static String uid(){
		String uid=session.get("sessionUser");
		return uid;
	}
	public static void addphoto(File photo) {
		
		//System.out.println(photo.getAbsolutePath());
		String photoname = Codec.UUID();
		//pser.pictures.add(photoname);
		photo.renameTo(new File("public/DataOfImages/"+photoname+".jpg"));
		User us=User.find("by_id",uid()).first();
		us.getPhoto().add(photoname+".jpg");
		int siz=us.getPhoto().size();
		if(siz==1){
			us.setPrPhoto(photoname+".jpg");
		}
		us.save();
		redirect("/photo");
    }
	public static void PrPhoto(){
		User us=User.find("by_id",uid()).first();
		us.setPrPhoto(request.params.get("p_name"));
		us.save();
	}
	public static void delete(){
		User us=User.find("by_id",uid()).first();
		String p_name=request.params.get("p_name");
		for(int i=0;i<us.getPhoto().size();i++){
			if(us.getPhoto().get(i).equals(p_name)){
				us.getPhoto().remove(i);
				us.save();
			}
				if(us.getPrPhoto().equals(p_name)){
					us.setPrPhoto("default_avatar.png");
					us.save();
					break;
				}
		}
	}
}
